package jcommon.chap5_collection;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.functors.EqualPredicate;
import org.apache.commons.lang.ArrayUtils;

public class CountAndCardinality {
	public static void main(String[] args) {
		System.out
				.println("-------- Test countMatches(), cardinality() --------");
		testCAndC();

		System.out.println("-------- Test getCardinalityMap() --------");
		testGetCardinalityMap();
	}

	private static void testCAndC() {
		String[] array = new String[] { "A", "B", "C", "C", "B", "B" };
		List stringList = Arrays.asList(array);

		Predicate equalsPredicate = new EqualPredicate("C");
		// ☆
		int numberCs = CollectionUtils
				.countMatches(stringList, equalsPredicate);
		// ☆
		int numberBs = CollectionUtils.cardinality("B", stringList);

		System.out.println("List: " + ArrayUtils.toString(array));
		System.out.println("Number of Cs: " + numberCs);
		System.out.println("Number of Bs: " + numberBs);
	}

	private static void testGetCardinalityMap() {
		String[] arrays = new String[] { "Red", "Blue", "Blue", "Yellow",
				"Red", "Black" };
		List stringList = Arrays.asList(arrays);

		Map cardinalityMap = CollectionUtils.getCardinalityMap(stringList);

		System.out.println("List: " + ArrayUtils.toString(arrays));
		MapUtils.debugPrint(System.out, "cardinalityMap", cardinalityMap);
	}
}
